package weaver.splittable.db.sqlserver;

import weaver.splittable.db.STCreateTriggerUtil;
import weaver.splittable.db.STDBConstants;

/**
 * 建触发器工具的sqlserver实现
 * author: YUEYANG 2023-01-12
 */
public class STSqlserverCreateTriggerUtil implements STCreateTriggerUtil {

    private static final String CREATE_TRIGGER_INSERT_SQL =
            "CREATE TRIGGER %s ON %s FOR INSERT AS\n" +
                    "declare @datetime varchar(32) \n" +
                    "SELECT @datetime = CONVERT(varchar(100), GETDATE(), 120)\n" +
                    "INSERT INTO "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" (event_type, table_name, data_id_column, data_id, synced, create_time, sync_time) \n" +
                    "select 'insert', '%s', '%s', %s, '0', @datetime, null from inserted;";

    private static final String CREATE_TRIGGER_UPDATE_SQL =
            "CREATE TRIGGER %s ON %s FOR UPDATE AS\n" +
                    "declare @datetime varchar(32) \n" +
                    "SELECT @datetime = CONVERT(varchar(100), GETDATE(), 120)\n" +
                    "INSERT INTO "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" (event_type, table_name, data_id_column, data_id, synced, create_time, sync_time) \n" +
                    "select 'update', '%s', '%s', %s, '0', @datetime, null from inserted;";

    private static final String CREATE_TRIGGER_DELETE_SQL =
            "CREATE TRIGGER %s ON %s FOR DELETE AS\n" +
                    "declare @datetime varchar(32) \n" +
                    "SELECT @datetime = CONVERT(varchar(100), GETDATE(), 120)\n" +
                    "INSERT INTO "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" (event_type, table_name, data_id_column, data_id, synced, create_time, sync_time) \n" +
                    "select 'delete', '%s', '%s', %s, '0', @datetime, null from deleted;";

    @Override
    public String createTriggerInsert(String tableName, String dataIdColumn) {
        String triggerInsertName = tableName + "_ins";
        if (tableName.length() > 20) {
            triggerInsertName = tableName.substring(0, 19) + "_ins";
        }
        return String.format(CREATE_TRIGGER_INSERT_SQL, triggerInsertName, tableName, tableName, dataIdColumn, dataIdColumn);
    }

    @Override
    public String createTriggerUpdate(String tableName, String dataIdColumn) {
        String triggerUpdateName = tableName + "_up";
        if (tableName.length() > 20) {
            triggerUpdateName = tableName.substring(0, 19) + "_up";
        }
        return String.format(CREATE_TRIGGER_UPDATE_SQL, triggerUpdateName, tableName, tableName, dataIdColumn, dataIdColumn);
    }

    @Override
    public String createTriggerDelete(String tableName, String dataIdColumn) {
        String triggerDeleteName = tableName + "_del";
        if (tableName.length() > 20) {
            triggerDeleteName = tableName.substring(0, 19) + "_del";
        }
        return String.format(CREATE_TRIGGER_DELETE_SQL, triggerDeleteName, tableName, tableName, dataIdColumn, dataIdColumn);
    }
}
